package com.strategies;

import com.model.Card;
import com.model.Hand;
import com.model.Rank;

public class PlayerStrategy1 implements IStrategy {

	@Override
	public boolean shouldSplit(Hand hand, Card dealerCard) {
		boolean dealerHasACE = dealerCard.rank().equals(Rank.ACE);
		int dealerCardValue = dealerCard.getValue();
		
		Card firstCard = hand.getFirstCard();
		Card secondCard = hand.getSecondCard();
		
		if (hand.size() == 2 && firstCard.getValue() == secondCard.getValue()) {
			if (firstCard.rank().equals(Rank.ACE) || firstCard.rank().equals(Rank.EIGHT)) {
				return true;
			}
			else if ((firstCard.rank().equals(Rank.TWO) || firstCard.rank().equals(Rank.THREE) || firstCard.rank().equals(Rank.SEVEN)) && dealerCardValue <= 7) {
				return true;
			}
			else if (firstCard.rank().equals(Rank.FOUR) && (dealerCardValue == 5 || dealerCardValue == 6)) {
				return true;
			}
			else if (firstCard.rank().equals(Rank.SIX) && dealerCardValue <= 6) {
				return true;
			}
			else if (firstCard.rank().equals(Rank.NINE) && (dealerCardValue == 7 || dealerCardValue == 10 || dealerHasACE)) {
				return true;
			}
			else {
				return false;
			}
		}
		return false;
	}

	@Override
	public boolean shouldDouble(Hand hand, Card dealerCard) {
		if (!hand.canDouble()) {
			return false;
		}
		
		boolean dealerHasACE = dealerCard.rank().equals(Rank.ACE);
		int playerHandValue = hand.getTotalHandValue();
		int dealerCardValue = dealerCard.getValue();
		
		if (hand.size() != 2) {
			return false;
		}
		
		if (hand.isSoft()) {
			if (dealerCardValue > 6) {
				return false;
			}
			else if (playerHandValue == 13 && dealerCardValue >= 5) {
				return true;
			}
			else if (playerHandValue == 14 && dealerCardValue >= 5) {
				return true;
			}
			else if (playerHandValue == 15 && dealerCardValue >= 4) {
				return true;
			}
			else if (playerHandValue == 16 && dealerCardValue >= 4) {
				return true;
			}
			else if (playerHandValue == 17 && dealerCardValue >= 3) {
				return true;
			}
			else if (playerHandValue == 18 && dealerCardValue >= 2) {
				return true;
			}
			else if (playerHandValue == 19 && dealerCardValue == 6) {
				return true;
			}
			else {
				return false;
			}
		}
		else if (playerHandValue == 11) {
			return true;
		}
		else if (playerHandValue == 10 && dealerCardValue <= 9 && !dealerHasACE) {
			return true;
		}		
		else if (playerHandValue == 9 && dealerCardValue <= 6 && dealerCardValue >= 3) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public boolean shouldHit(Hand hand, Card dealerCard) {
		boolean dealerHasACE = dealerCard.rank().equals(Rank.ACE);
		int playerHandValue = hand.getTotalHandValue();
		int dealerCardValue = dealerCard.getValue();
		
		if (hand.isSoft()) {			
			if (playerHandValue <= 17) {
				return true;
			}
			else if (playerHandValue >= 19) {
				return false;
			}
			else if (playerHandValue == 18 && (dealerHasACE || dealerCardValue >= 9)) {
				return true;
			}
			else {
				return false;
			}
		}
		else {
			if (playerHandValue <= 11 ) {
				return true;
			}
			else if (playerHandValue >= 17 ) {
				return false;
			} 	
//			else if (getHand().size() >= 4) {
//				return false; //Test! Dont hit if your hand contains 4 or more cards
//			}				
			else if (playerHandValue == 12 && dealerCardValue == 3 && dealerCardValue == 2) {
				return true;
			}		
			else if (playerHandValue <= 16 && dealerCardValue <= 6 && !dealerHasACE) {
				return false;
			}
			else {
				return true;
			}
		}	
	}

}
